package cn.nawang.ebeim.api.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.StorageCloud;
import cn.nawang.ebeim.api.domain.StorageStrategy;
import cn.nawang.ebeim.api.repository.StorageCloudRepository;
import cn.nawang.ebeim.api.repository.impl.StorageCloudRepositoryImpl;
import cn.nawang.ebeim.api.service.StorageCloudService;
import cn.nawang.ebeim.api.service.StorageStrategyService;
import cn.nawang.ebeim.api.utils.ConvertUtil;

/**
 * @ClassName: StorageCloudServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Aug 18, 2015 9:53:12 AM
 * @version v1.0
 */
@Service("storageCloudService")
public class StorageCloudServiceImpl implements StorageCloudService {

  @Autowired
  private StorageCloudRepositoryImpl storageCloudRepositoryImpl;

  @Autowired
  private StorageCloudRepository storageCloudRepository;

  @Autowired
  private StorageStrategyService storageStrategyService;

  @Override
  public List<StorageCloud> findUserStorageCloudList(String customerId) {
    List<StorageCloud> list = null;
    StorageStrategy storageStrategy = storageStrategyService.findByCustomerId(customerId);
    String param = "";
    if (storageStrategy != null) {
      param += "('" + storageStrategy.getStorageCloudId1() + "',";
      param += "'" + storageStrategy.getStorageCloudId2() + "',";
      param += "'" + storageStrategy.getStorageCloudId3() + "')";
      list = storageCloudRepositoryImpl.findStorageCloudById(param);
      return list;
    }
    return list;
  }

  @Override
  public List<StorageCloud> findByParam(String[] params) {
    List<StorageCloud> list =
        storageCloudRepositoryImpl.findStorageCloudById(ConvertUtil.toDbString(params));
    return list;
  }

  @Override
  public List<StorageCloud> findByProperty(String property, String value) {
    List<StorageCloud> list = storageCloudRepositoryImpl.findByParam(property, value);
    return list;
  }

  @Override
  public StorageCloud findByStorageCloudName(String name) {
    String property = "name";
    StorageCloud storageCloud = null;
    List<StorageCloud> list = this.findByProperty(property, name);
    if (!list.isEmpty() && list != null) {
      storageCloud = list.get(0);
    }
    return storageCloud;
  }

  @Override
  public boolean add(StorageCloud storageCloud) {
    try {
      storageCloud.setId("SC-" + new Date().getTime() + "-" + new Random().nextInt(1000));
      storageCloudRepository.save(storageCloud);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public PageList findPageList(Pages page) {
    return storageCloudRepositoryImpl.findAllByPaging(page);
  }

  @Override
  public StorageCloud findOne(String id) {
    return storageCloudRepository.findOne(id);
  }

  @Override
  public boolean delete(String[] ids) {
    return storageCloudRepositoryImpl.delete(ids);
  }

  @Override
  public boolean update(StorageCloud storageCloud) {
    try {
      storageCloudRepository.save(storageCloud);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

}
